﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZMenFM.Network.Spider;

namespace ZMenFM.Network
{
    static public class ZMNetworkGlobal
    {
        public const String FromVar = "ZMenFM";
        static private bool Inited = false;
        static private ZMSpider Spider;

        /// <summary>
        /// 获取爬虫对象
        /// </summary>
        /// <returns>爬虫对象</returns>
        static public ZMSpider GetSpider()
        {
            if (!Inited)
            {
                try
                {
                    InitEnviroment();
                }
                catch(Exception)
                {
                    return null;
                }
            }

            return Spider;
        }

        /// <summary>
        /// 初始化环境
        /// </summary>
        static public void InitEnviroment()
        {
            Spider = new ZMSpider();

            if (!Spider.InitContext())
            {
                Exception e = new Exception("ZMNetwork failed to initialize.");
                throw e;
            }

            Inited = true;
        }

        /// <summary>
        /// 将URL进行Encode
        /// </summary>
        /// <param name="str">原字符串</param>
        /// <returns>Encode之后的字符串</returns>
        public static string UrlEncode(string str)
        {
            return Uri.EscapeUriString(str);
        }

        /// <summary>
        /// 将URL进行Decode
        /// </summary>
        /// <param name="str">原字符串</param>
        /// <returns>Decode之后的字符串</returns>
        public static string UrlDecode(string str)
        {
            return Uri.EscapeDataString(str);
        }
    }
}
